﻿using SuYunExpressPackageSystem.BLL;
using SuYunExpressPackageSystem.Models;
using SuYunExpressPackageSystem.Utility;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SuYunExpressPackageSystem
{
    public partial class UserMain : Form
    {
        private ChatBot chatBot;  // 用于获取答案
        private Panel panel;
        private EmpTypeBLL empTypeBLL = new EmpTypeBLL();

        public UserMain()
        {
            InitializeComponent();
            chatBot = new ChatBot();
        }

        private void Button3_Click(object sender, EventArgs e)
        {
            this.panel1.Hide();
            panel = new Panel()
            {
                Width = 985,
                Height = 600,
                BackColor = Color.White,
                Location = new Point(151, -1)
            };
            TextBox textBox = new TextBox()
            {
                Width = 820,
                Height = 38,
                Location = new Point(66, 515),
                Multiline = true,
                BackColor = Color.WhiteSmoke,
                BorderStyle = BorderStyle.Fixed3D,
                Font = new Font("Arial", 14)
            };
            Button button = new Button()
            {
                Location = new Point(880, 515),
                Size = new Size(99, 40),
                Text = "发送",
                Font = new Font("Arial", 10)
            };
            RichTextBox textBox2 = new RichTextBox()
            {
                Location = new Point(66, 43),
                Size = new Size(820, 454),
                Multiline = true,
                Font = new Font("Arial", 12),
                SelectionIndent = 18
            };

            this.Controls.Add(panel);
            panel.Controls.Add(button);
            panel.Controls.Add(textBox);
            panel.Controls.Add(textBox2);
            // 监听 TextBox 的 KeyDown 事件，捕捉回车键
            textBox.KeyDown += (s, args) =>
            {
                if (args.KeyCode == Keys.Enter)
                {
                    // 模拟点击事件
                    button.PerformClick();
                }
            };
            button.Click += (s, args) => Button_Click(s, args, textBox, textBox2);
        }

        private async void Button_Click(object sender, EventArgs e, TextBox textBox, RichTextBox textBox2)
        {
            string userQuestion = textBox.Text.Trim();
            List<ExpressInfo> data = empTypeBLL.GetEmployeeInfoss(userQuestion);

            if (!string.IsNullOrEmpty(userQuestion))
            {
                // 显示用户提问
                textBox2.SelectionAlignment = HorizontalAlignment.Right;
                textBox2.AppendText($"{userQuestion}\n\n");

                if (data.Count != 0)
                {
                    textBox2.AppendText("小速 ：");

                    for (int i = 0; i < data.Count; i++)
                    {
                        // 显示 AI 回复
                        await OutputTextWithDelayAsync(textBox2, $"{data[i].Receiver}, 您的订单编号{data[i].ExpNumber}的{data[i].ExpRemark}物品，状态是{data[i].ExpState}地址是从{data[i].SendAddress}  到  {data[i].ReceiveAddress}。\n", HorizontalAlignment.Left, 33);
                    }

                    return;
                }
                if (userQuestion.All(Char.IsDigit))
                {
                    textBox2.AppendText("小速 ：");
                    await OutputTextWithDelayAsync(textBox2, "输入有错，请问重新输入", HorizontalAlignment.Left, 33);
                    return;
                }
                // 获取 AI 回复
                string aiAnswer = chatBot.GetAnswer(userQuestion);
                // 清空文本框
                textBox.Clear();
                // 显示 AI 回复
                textBox2.AppendText("小速 ：");
                await OutputTextWithDelayAsync(textBox2, $"{aiAnswer}\n\n", HorizontalAlignment.Left, 66);
                textBox2.ScrollToCaret();
            }
        }

        // 逐字输出方法
        private async Task OutputTextWithDelayAsync(RichTextBox richTextBox, string text, HorizontalAlignment alignment, int delay)
        {
            richTextBox.SelectionAlignment = alignment;

            foreach (char c in text)
            {
                // 添加字符到 RichTextBox
                richTextBox.AppendText(c.ToString());

                //   强制刷新 RichTextBox（可选）
                richTextBox.Refresh();

                // 延迟显示
                await Task.Delay(delay);
            }

            // 在逐字输出完成后添加换行
            richTextBox.AppendText(Environment.NewLine);
            richTextBox.ScrollToCaret();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (panel1 == null || panel == null)
            {
                return;
            }
            this.panel.Hide();
            this.panel1.Show();
        }

        private void querybtn_Click(object sender, EventArgs e)
        {
            richTextBox1.Clear();
            if (textBox1.Text != "")
            {
                List<ExpressInfo> data = empTypeBLL.GetEmployeeInfoss(textBox1.Text);
                for (int i = 0; i < data.Count; i++)
                {
                    richTextBox1.AppendText($"快递单号:{data[i].ExpNumber}  货架号:{data[i].StationId}   物品:{data[i].ExpRemark}   类型:{data[i].ExpType}  物品状态:{data[i].ExpState}。\n");
                    richTextBox1.AppendText($"寄件人:{data[i].Sender} 电话:{data[i].SenderPhone}  地址:{data[i].SendAddress}。\n");
                    richTextBox1.AppendText($"收件人:{data[i].Receiver}  电话:{data[i].ReceiverPhone}  地址:{data[i].ReceiveAddress}。\n\n");
                }
            }
        }

        private void UserMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }
    }
}